﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Scrum.SwimLanes.Models
{
    public class SettingsHelper
    {
        private static Dictionary<string, List<int>> _teamProjectSprintMap { get; set; }

        public static Dictionary<string, List<int>> TeamProjectSprintMap
        {
            get
            {
                if (_teamProjectSprintMap != null) return _teamProjectSprintMap;
                else
                {
                    Dictionary<string, List<int>> teamProjectSprintMap = new Dictionary<string, List<int>>();

                    string teamProjectListString = System.Configuration.ConfigurationManager.AppSettings["TeamProjectList"].ToString();
                    string[] teamProjectList = default(string[]);

                    if (!string.IsNullOrEmpty(teamProjectListString))
                    {
                        teamProjectList = teamProjectListString.Split(',');
                        foreach (var project in teamProjectList)
                        {
                            var sprintList = new List<int>();
                            int pIndex = project.IndexOf('>');
                            string sprintsToTrack = project.Substring(project.IndexOf('>') + 1, project.Length - project.IndexOf('>') - 1);
                            sprintList.AddRange(sprintsToTrack.Split('+').Select(i => Convert.ToInt32(i)));
                            teamProjectSprintMap.Add(project.Substring(0, pIndex).Trim(), sprintList);
                        }
                    }
                    _teamProjectSprintMap = teamProjectSprintMap;
                    return _teamProjectSprintMap;
                }                
            }
            
        }
    }
}